// ==========================================================================
// GFast自动生成logic操作代码。
// 生成日期：2024-06-18 17:21:41
// 生成路径: internal/app/app/logic/app_news.go
// 生成人：gfast
// desc:文章
// company:云南奇讯科技有限公司
// ==========================================================================

package appNews

import (
	"context"

	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/tiger1103/gfast/v3/internal/app/app/dao"
	"github.com/tiger1103/gfast/v3/internal/app/app/model"
	"github.com/tiger1103/gfast/v3/internal/app/app/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/app/service"
	"github.com/tiger1103/gfast/v3/internal/app/system/consts"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterAppNews(New())
}

func New() service.IAppNews {
	return &sAppNews{}
}

type sAppNews struct{}

func (s *sAppNews) List(ctx context.Context, req *model.AppNewsSearchReq) (listRes *model.AppNewsSearchRes, err error) {
	listRes = new(model.AppNewsSearchRes)
	err = g.Try(ctx, func(ctx context.Context) {
		m := dao.AppNews.Ctx(ctx).WithAll()
		if req.CategoryId != "" {
			m = m.Where(dao.AppNews.Columns().CategoryId+" = ?", gconv.Uint(req.CategoryId))
		}
		if req.Title != "" {
			m = m.Where(dao.AppNews.Columns().Title+" like ?", "%"+req.Title+"%")
		}
		listRes.Total, err = m.Count()
		liberr.ErrIsNil(ctx, err, "获取总行数失败")
		if req.PageNum == 0 {
			req.PageNum = 1
		}
		listRes.CurrentPage = req.PageNum
		if req.PageSize == 0 {
			req.PageSize = consts.PageSize
		}
		order := "id desc"
		if req.OrderBy != "" {
			order = req.OrderBy
		}
		err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&listRes.List)
		liberr.ErrIsNil(ctx, err, "获取数据失败")
	})
	return
}

// LinkedDataSearch 相关连表查询数据
func (s *sAppNews) LinkedAppNewsDataSearch(ctx context.Context) (res *model.LinkedAppNewsDataSearchRes, err error) {
	res = new(model.LinkedAppNewsDataSearchRes)
	res.LinkedAppNewsAppNewsCategory, err = s.ListAppNewsAppNewsCategory(ctx)
	liberr.ErrIsNil(ctx, err, "获取关联表信息失败")
	return
}

func (s *sAppNews) GetById(ctx context.Context, id uint64) (res *model.AppNewsInfoRes, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.AppNews.Ctx(ctx).WithAll().Where(dao.AppNews.Columns().Id, id).Scan(&res)
		liberr.ErrIsNil(ctx, err, "获取信息失败")
		if res != nil {
		}
	})
	return
}

func (s *sAppNews) Add(ctx context.Context, req *model.AppNewsAddReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNews.Ctx(ctx).Insert(do.AppNews{
			CategoryId: req.CategoryId,
			Title:      req.Title,
			Author:     req.Author,
			Thumb:      req.Thumb,
			Status:     req.Status,
			Content:    req.Content,
		})
		liberr.ErrIsNil(ctx, err, "添加失败")
	})
	return
}

func (s *sAppNews) Edit(ctx context.Context, req *model.AppNewsEditReq) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNews.Ctx(ctx).WherePri(req.Id).Update(do.AppNews{
			CategoryId: req.CategoryId,
			Title:      req.Title,
			Author:     req.Author,
			Thumb:      req.Thumb,
			Status:     req.Status,
			Content:    req.Content,
		})
		liberr.ErrIsNil(ctx, err, "修改失败")
	})
	return
}

func (s *sAppNews) Delete(ctx context.Context, ids []uint64) (err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		_, err = dao.AppNews.Ctx(ctx).Delete(dao.AppNews.Columns().Id+" in (?)", ids)
		liberr.ErrIsNil(ctx, err, "删除失败")
	})
	return
}

func (s *sAppNews) ListAppNewsAppNewsCategory(ctx context.Context) (linkedAppNewsAppNewsCategory []*model.LinkedAppNewsAppNewsCategory, err error) {
	err = g.Try(ctx, func(ctx context.Context) {
		err = dao.AppNewsCategory.
			Ctx(ctx).
			Fields(model.LinkedAppNewsAppNewsCategory{}).Scan(&linkedAppNewsAppNewsCategory)
		liberr.ErrIsNil(ctx, err)
	})
	return
}
